/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package analex;

import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import view.frame_CodigoFonte;

/**
 *
 * @author Pedrom
 */
public class FrameManager {

    private frame_CodigoFonte frame;
    private String tokens;
    private String erros;

    public FrameManager() {        
    }
    
    public void start(){
        this.frame = new frame_CodigoFonte(this);
        frame.setVisible(true);
    }

    public void analex(String code) {
        try {
            Spliter sp = new Spliter();
            sp.split(code);
            tokens = this.atualizarFrameTokens(sp);
            erros = this.atualizarFrameErro(sp);
            frame.atualizaToken(tokens);
            frame.atualizaErro(erros);

        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String atualizarFrameTokens(Spliter sp) {
        LinkedList<Token> tokens = sp.getListaToken();

        StringBuilder bs = new StringBuilder();
        for (Token token : tokens) {
            bs.append(token.getTipo());
            bs.append(" - ");
            bs.append(token.getValue());
            bs.append(" linha:");
            bs.append(token.getLine());
            bs.append("\n");
        }
        return bs.toString();

    }

    public String atualizarFrameErro(Spliter sp) {
        LinkedList<Erro> lista = sp.getListaErro();

        StringBuilder bs = new StringBuilder();
        for (Erro er : lista) {
            bs.append(er.getDescricao());
            bs.append(" - ");
            bs.append(er.getLexema());
            bs.append(" linha:");
            bs.append(er.getLinha());
            bs.append("\n");
        }
        return bs.toString();

    }
}
